package com.xique.blelock.NetUtils;

/**
 * Created by Administrator on 2018/10/15.
 */

import android.provider.SyncStateContract;

import com.xique.blelock.Utils.ACache;
import com.xique.blelock.model.FingerInfo;
import com.xique.blelock.model.HistoryMessageInfo;
import com.xique.blelock.model.LockAllinfo;
import com.xique.blelock.model.LockInfo;
import com.xique.blelock.model.Operationinfo;
import com.xique.blelock.model.UnreadMessage;
import com.xique.blelock.model.UserNetInfo;
import com.xique.blelock.model.UserOfLockinfo;

import org.json.JSONObject;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.fastjson.FastJsonConverterFactory;
import retrofit2.http.Body;


/**
 * Created by Wxm on 2016/11/10.
 */

public class XqNetClient {
    private XqNetService service;

    public XqNetClient(String module) {
        service = getService();
    }

    public XqNetClient() {
        service = getService();
    }

    private XqNetService getService() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.readTimeout(1, TimeUnit.MINUTES);
//        builder.interceptors().add(new AddCookiesInterceptor());
//        builder.interceptors().add(new ReceivedCookiesInterceptor());

        //添加代理 47.107.227.104


        OkHttpClient client = builder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(XqNetService.URL_SERVICE)
                .client(client)
                .addConverterFactory(FastJsonConverterFactory.create())
                .build();
        return retrofit.create(XqNetService.class);
    }

   /* private XqNetService getService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MySettings.REQUESTURI)  //主url
                .addConverterFactory(GsonConverterFactory.create())//gson解析
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//rxjava
                .build();
        Api api = retrofit.create(Api.class);
        Observable<bean> observable = api.addUser(url, RequestBody 对象);
        observable.subscribeOn(Schedulers.io())                 //访问接口时的线程
                .unsubscribeOn(AndroidSchedulers.mainThread())  //链接开始前和异常结束的线程选择
                .observeOn(AndroidSchedulers.mainThread())      //获取到数据时的线程
                .subscribe(new Subscriber() {


                });
    }*/
    /**
     * Cookie持久化
     */
//    private class ReceivedCookiesInterceptor implements Interceptor {
//        @Override
//        public Response intercept(Chain chain) throws IOException {
//            Response originalResponse = chain.proceed(chain.request());
//
//            if (!originalResponse.headers("Set-Cookie").isEmpty()) {
//                List<String> cookies = originalResponse.headers("Set-Cookie");
//
//                String cookie = "";
//                for (String s : cookies) {
//                    cookie += s;
//                }
//                ACache.getInstance().put(Constants.COOKIE, cookie);
//            }
//            return originalResponse;
//        }
//    }

    private class AddCookiesInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request.Builder builder = chain.request().newBuilder();
//            String c = ACache.getInstance().getAsString(SyncStateContract.Constants.COOKIE);
//            if (c != null) {
//                builder.addHeader("Cookie", c);
//            }
            builder.addHeader("Authorization", "2f010fb41bd747baade3dcd80682c260");
            return chain.proceed(builder.build());
        }
    }
  /*  public Observable<BeanContext> sendAuthCode1(RequestBody body) {
        return getService().sendAuthCode1(body);
    }*/

    public Call<String> sendAuthCode(String dat) {
        return getService().sendAuthCode(dat);
    }

    public Call<String> sendAuthCode2(String dat) {
        return getService().sendAuthCode2(dat);
    }

    public Call<String> Login(String dat) {
        return getService().Login(dat);
    }

    public Call<String> sendAuthCode4(String s) {
        return getService().sendAuthCode4(s);
    }
    public Call<String> changePassword(String s) {
        return getService().changePassword(s);
    }

    //设置密码setPassword
    public Call<NetResult> setPassword(String s) {
        return getService().setPassword(s);
    }


    //反馈意见

    public Call<NetResult> sendSuggestiions(String s) {
        return getService().sendSuggestiions(s);
    }
    /**
     * 重置密码
     * @param s
     * @return
     */
    public Call<String> ReSetpasswd(String s) {
        return getService().ReSetpasswd(s);
    }

    /**
     * 查询用户
     * @param s
     * @return
     */
    public Call<String> queryUserInfo(String s) {
        return getService().queryUserInfo(s);
    }

    /**
     * 设置user
     * @param s
     * @return
     */
    public Call<String> updateUserInfo(String s) {
        return getService().updateUserInfo(s);
    }
    /*public Call<String> sendAuthCode(String phone) {
        return getService().sendAuthCode(phone);
    }*/


    //查询设备列表
    public Call<NetResult<List<LockInfo>>> queryLockList(String paramEntity) {
        return getService().queryLockList(paramEntity);
    }

    //增加设备
    public Call<NetResult> addLock(String paramEntity) {
        return getService().addLock(paramEntity);
    }

    //更新锁的信息
    public Call<NetResult> updateLockInfo(String paramEntity) {
        return getService().updateLockInfo(paramEntity);
    }
    //增加设备
    public Call<NetResult> deleteLock(String paramEntity) {
        return getService().deleteLock(paramEntity);
    }

    //updateLockaName
    public Call<NetResult> updateLockaName(String paramEntity) {
        return getService().updateLockaName(paramEntity);
    }

    public Call<NetResult<List<Operationinfo>>> queryLockRecords(String paramEntity) {
        return getService().queryLockRecords(paramEntity);
    }

    //@POST(queryLockRecords)
    //Call<NetResult<List<Operationinfo>>> queryLockRecordsByTime(@Body String paramEntity);
    public Call<NetResult<List<Operationinfo>>> queryLockRecordsByTime(String paramEntity) {
        return getService().queryLockRecordsByTime(paramEntity);
    }

    //添加操作记录addLockRecords
    public Call<NetResult> addLockRecords(String paramEntity) {
        return getService().addLockRecords(paramEntity);
    }

    //查询锁与用户的信息
    public Call<NetResult<LockAllinfo>> queryLockInfo(String paramEntity) {
        return getService().queryLockInfo(paramEntity);
    }



    public Call<NetResult<List<FingerInfo>>> getFigerList(String paramEntity) {
        return getService().getFigerList(paramEntity);
    }

    //更新指纹列表updateFinger
    public Call<NetResult> updateFinger(String paramEntity) {
        return getService().updateFinger(paramEntity);
    }


    //检查连接密码checkDeviceConneCode
    public Call<NetResult<String>> checkDeviceConneCode(String paramEntity) {
        return getService().checkDeviceConneCode(paramEntity);
    }


    //添加指纹列表addFingerList
    public Call<NetResult> addFingerList(String paramEntity) {
        return getService().addFingerList(paramEntity);
    }
    //删除指纹列表 deletFingerLisst
    public Call<NetResult> deletFingerLisst(String paramEntity) {
        return getService().deletFingerLisst(paramEntity);
    }

    public Call<NetResult<List<UserOfLockinfo>>> getUserList(String paramEntity) {
        return getService().getUserList(paramEntity);
    }



    public Call<NetResult<UserNetInfo>> getUserByPhone(String paramEntity) {
        return getService().getUserByPhone(paramEntity);
    }


    public Call<NetResult> addBandUser(String paramEntity) {
        return getService().addBandUser(paramEntity);
    }
    public Call<NetResult> deleteBandUser(String paramEntity) {
        return getService().deleteBandUser(paramEntity);
    }


    //设置管理员

    public Call<NetResult> setAdmin(String paramEntity) {
        return getService().setAdmin(paramEntity);
    }

    public Call<NetResult> cancleAdmin(String paramEntity) {
        return getService().cancleAdmin(paramEntity);
    }

    //getUnreadMessageCount
    public Call<NetResult<List<UnreadMessage>>> getUnreadMessageCount(String paramEntity) {
        return getService().getUnreadMessageCount(paramEntity);
    }

    public Call<NetResult<List<HistoryMessageInfo>>> getHistoryMessages(String paramEntity) {
        return getService().getHistoryMessages(paramEntity);
    }

    public Call<NetResult> setMessageIsRead(String paramEntity) {
        return getService().setMessageIsRead(paramEntity);
    }

    public Call<NetResult> deleteMessages(String paramEntity) {
        return getService().deleteMessages(paramEntity);
    }


//
//    public Call<FxCommonResult<Object>> forgetPassword(ParamEntity paramEntity) {
//        return getService().forgetPassword(paramEntity);
//    }
//
//    public Call<FxCommonResult<UserStudent>> login(ParamEntity paramEntity) {
//        return getService().login(paramEntity);
//    }
//
//
//    public Call<FxCommonResult<UserStudent>> loginQuickly(QuicklyLoginVo paramEntity) {
//        return getService().loginQuickly(paramEntity);
//    }





//    //图片上传UpLoadImg
//    public Call<FxCommonResult<String>> UpLoadImg(@Part MultipartBody.Part file) {
//        return getService().UpLoadImg(file);
//    }













}

